Skip to content

feat(logger): middy middleware #313

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 14 commits into from
Dec 17, 2021
Merged

feat(logger): middy middleware #313

merged 14 commits into from
Dec 17, 2021

Conversation

saragerion
Copy link
Contributor

@saragerion saragerion commented Dec 14, 2021

Description of your changes

Added:

  • Middy middleware functionality, unit tests, example, documentation
  • Small update to the cold start detection business logic, bug captured during the middleware unit test implementation
  • Removed dummy "deadbeef" references in dummy events, improved consistency of values in test events
  • Fixed linting issues in the logger folder
  • Fixed issue with github workflows

How to verify this change

  1. Build the doc with npm run docs-runLocalDocker
  2. Go to http://localhost:8000/core/logger/

Related issues, RFCs

#294

PR status

Is this ready for review?: YES
Is it a breaking change?: NO

Checklist

  • My changes meet the tenets criteria
  • I have performed a self-review of my own code
  • I have commented my code where necessary, particularly in areas that should be flagged with a TODO, or hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • The code coverage hasn't decreased
  • I have added tests that prove my change is effective and works
  • New and existing unit tests pass locally and in Github Actions
  • Any dependent changes have been merged and published in downstream module
  • The PR title follows the conventional commit semantics

Breaking change checklist

N/A


By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

@dreamorosi dreamorosi added enhancement logger This item relates to the Logger Utility labels Dec 15, 2021
@dreamorosi dreamorosi added this to the beta-release milestone Dec 15, 2021
@@ -116,7 +134,7 @@ In both case, the printed log will look like this:
"cold_start": true,
"function_arn": "arn:aws:lambda:eu-central-1:123456789012:function:shopping-cart-api-lambda-prod-eu-central-1",
"function_memory_size": 128,
"function_request_id": "c6af9ac6-7b61-11e6-9a41-93e8deadbeef",
"function_request_id": "c6af9ac6-7b61-11e6-9a41-93e812345678",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Small change I did in this PR: removed all these weird "deadbeef" references


```typescript
// Environment variables set for the Lambda
process.env.LOG_LEVEL = 'INFO';
process.env.POWERTOOLS_SERVICE_NAME = 'hello-world';
import { injectLambdaContext } from '../src/middleware/middy';
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will update the relative paths of the examples in the readme file in another PR

@@ -7,7 +7,7 @@ process.env.POWERTOOLS_SERVICE_NAME = 'hello-world';

import * as dummyEvent from '../../../tests/resources/events/custom/hello-world.json';
import { context as dummyContext } from '../../../tests/resources/contexts/hello-world';
import { LambdaInterface } from './utils/lambda/LambdaInterface';
import { LambdaInterface } from '../types/LambdaInterface';
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@flochaz the examples folder referenced the types because of this one.
I move the LambdaInterface inside the types

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok

@@ -13,7 +13,8 @@
"resolveJsonModule": true,
"pretty": true,
"baseUrl": "src/",
"rootDirs": [ "src/" ]
"rootDirs": [ "src/" ],
"esModuleInterop": true
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@@ -32,7 +32,7 @@
"Host": "1234567890.execute-api.eu-central-1.amazonaws.com",
"Upgrade-Insecure-Requests": "1",
"User-Agent": "Custom User Agent String",
"Via": "1.1 08f323deadbeefa7af34d5feb414ce27.cloudfront.net (CloudFront)",
"Via": "1.1 08f32312345678a7af34d5feb414ce27.cloudfront.net (CloudFront)",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is again the "deadbeef" reference I replaced, in case you wonder

@saragerion saragerion linked an issue Dec 16, 2021 that may be closed by this pull request
@saragerion saragerion marked this pull request as ready for review December 16, 2021 02:11
dreamorosi
dreamorosi previously approved these changes Dec 16, 2021
Copy link
Contributor

@dreamorosi dreamorosi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great job and thanks for the heavy lifting with the first middleware implementation!

@@ -16,7 +16,7 @@ jobs:
node-version: '14'
- name: Install packages
run: |
npm ci
npm install
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh nice catch

import { Logger } from "@aws-lambda-powertools/logger";
import { injectLambdaContext } from '@aws-lambda-powertools/logger/middleware/middy';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we expose it at Top level, such as import { Middleware } from '@aws-lambda-powertools/logger'; ?

logger.info("This is an INFO log with some context");
};

const handler = middy(lambdaHandler)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

middy not defined

@@ -91,6 +92,9 @@ const lambdaHandler: Handler = async () => {

};

const handlerWithMiddleware = middy(lambdaHandler)
.use(injectLambdaContext(logger));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure of the naming here, can't we get it from logger logger.Middleware. injectLambdaContext() instead ? otherwise how do I know it's a logger middleware ?

Copy link
Contributor Author

@saragerion saragerion Dec 16, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting observation. So here is my reasoning:

That being said, your observation still stands. How do I know it's a logger middleware? For now, through the explicit import from the Logger package, a mix IDE tooltips, the explicit type of the middleware's parameters (target: Logger | Logger []).

Screenshot 2021-12-16 at 21 07 47

In the future, to remove code duplication, I could see this middleware being moved in the commons package and becoming "package agnostic": the middleware takes a logger instance, or a metrics instance, and so on, any instance that implements a common interface with the addContext method.
What do you think?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sounds good !

@@ -7,7 +7,7 @@ process.env.POWERTOOLS_SERVICE_NAME = 'hello-world';

import * as dummyEvent from '../../../tests/resources/events/custom/hello-world.json';
import { context as dummyContext } from '../../../tests/resources/contexts/hello-world';
import { LambdaInterface } from './utils/lambda/LambdaInterface';
import { LambdaInterface } from '../types/LambdaInterface';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok

@@ -91,6 +92,9 @@ const lambdaHandler: Handler = async () => {

};

const handlerWithMiddleware = middy(lambdaHandler)
.use(injectLambdaContext(logger));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sounds good !

@dreamorosi dreamorosi self-requested a review December 17, 2021 10:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
logger This item relates to the Logger Utility
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Feature request: logger middy middleware support
3 participants